package umich.max.geolocation.featextract.ner;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import umich.max.geolocation.geonames.Geonames;

import max.nlp.dal.blog.blogger.BlogAuthorProfile;
import max.nlp.dal.blog.blogger.BloggerBlog;
import max.nlp.dal.blog.blogger.BloggerDB;
import max.nlp.dal.blog.blogger.BloggerPost;
import max.nlp.dal.blog.blogger.ParsedLocation;
import max.nlp.wrappers.ml.weka.WekaFeatureExtractor;

public class PercentLocsInStateExtractor extends
		WekaFeatureExtractor<String, Double> {

	private static String prefix = "perLocs_";
	private Geonames geonames = new Geonames();
	private BloggerDB db = BloggerDB.getInstance();

	public PercentLocsInStateExtractor() {
		super(prefix);
	}

	public HashMap<String, Double> extractFeaturesForObject(Object item) {
		BloggerBlog blog = (BloggerBlog) item;
		HashMap<String, Double> locsInBlog = new HashMap<String, Double>();

		double numLocsInState = 0;
		double allLocs = 0;
		for (BloggerPost post : blog.getPosts()) {
			List<String> locations = post.getLocations();
			if (!locations.isEmpty()) {
				BlogAuthorProfile profile = db.findProfileForBlog(blog);
				ParsedLocation pLoc = profile.getParsedLocation();

				if (pLoc != null && pLoc.getState() != null) {
					String state = pLoc.getState();
					allLocs+=locations.size();
					for (String loc : locations) {
						HashSet<String> states = geonames
								.extractStatesFromQuery(loc);
						if (states.contains(state)) {
							numLocsInState++;
						}
					}
				}
			}
		}
		double perLocsInState = (numLocsInState / allLocs) * 100f;
		locsInBlog.put(prefix, perLocsInState);

		return locsInBlog;
	}

}
